//
//  LoginVC.m
//  Running
//
//  Created by 郑东喜 on 2016/10/30.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

#import "LoginVC.h"
#import "RigisterVC.h"
#import "ForgetVC.h"
#import "Encryption.h"
#import "MBProgressHUD+LJ.h"
#import "WKWebViewController.h"

@interface LoginVC ()<UITextFieldDelegate,CAAnimationDelegate>

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //http://bzg.ie1e.com/APP/User_UserLogin/Login?eid=微信号&tel=手机号&pwd=密码

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    BOOL status =  [stan boolForKey:savetoLocal];
    
    //取出本地存储状态
    self.status = status;
    
    self.remImg.hidden = [stan boolForKey:savetoLocal];
    
    
    NSLog(@"%s  %d  %zd",__func__,__LINE__,[stan boolForKey:savetoLocal]);
    
    if ([stan boolForKey:savetoLocal] == false) {
        
        self.tfNum.text = [stan objectForKey:uName];
        self.tfPass.text = [stan objectForKey:uPassword];
    } else {
        
        self.tfNum.text = nil;
        self.tfPass.text = nil;
    }
    
    //设置tag
    [self.tfNum setTag:220];
    [self.tfPass setTag:221];
}

#pragma mark - 本地存储
- (void)saveLocalInfo {
    if (self.status == false) {
        [stan setObject:self.tfNum.text forKey:uName];
        [stan setObject:self.tfPass.text forKey:uPassword];
    } else {
        [stan removeObjectForKey:uName];
        [stan removeObjectForKey:uPassword];
    }
    
    [stan synchronize];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)rigister:(UIButton *)sender {
    RigisterVC *rig = [[RigisterVC alloc] init];
    [self.navigationController pushViewController:rig animated:YES];
}

- (IBAction)forget:(UIButton *)sender {
    ForgetVC *forVC = [[ForgetVC alloc] init];
//    [self.navigationController pushViewController:forVC animated:YES];
    
    
    CATransition *caTransition = [CATransition animation];
    caTransition.duration = 0.5;
    caTransition.delegate = self;
    
    //    linear, easeIn, easeOut, easeInEaseOut, default
    caTransition.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"];//切换速度效果
    
    //    kCATransitionFade, kCATransitionMoveIn
    //    kCATransitionPush, kCATransitionReveal
    caTransition.type = kCATransitionFade;//动画切换风格
    
    //    kCATransitionFromRight, kCATransitionFromLeft
    //    kCATransitionFromTop, kCATransitionFromBottom
    caTransition.subtype = kCATransitionFromRight;//动画切换方向
    
    [self.navigationController pushViewController:forVC animated:YES];
    [self.navigationController.view.layer addAnimation:caTransition forKey:@"animation"];

}

- (IBAction)remChoose:(UIButton *)sender {
    sender.selected = ![stan boolForKey:savetoLocal];
    
    if (sender.isSelected) {
        self.remImg.hidden = YES;
        [stan setBool:sender.isSelected forKey:savetoLocal];
        
        [stan removeObjectForKey:uName];
        [stan removeObjectForKey:uPassword];
        
    } else {
        self.remImg.hidden = NO;
        [stan setBool:sender.isSelected forKey:savetoLocal];
        
        [stan setObject:self.tfNum.text forKey:uName];
        [stan setObject:self.tfPass.text forKey:uPassword];
    }
    
    [stan synchronize];
}


#pragma mark - uitextfieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    switch (textField.tag) {
        case 220:
            [self.tfNum resignFirstResponder];
            [self.tfPass becomeFirstResponder];
            break;
        case 221:
            [self.tfPass resignFirstResponder];
            
            break;
        default:
            break;
    }
    return YES;

}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    [self saveLocalInfo];
    return YES;
}



- (IBAction)loginSEl:(UIButton *)sender {
    [self.tools loginSEL:self.tfNum withPassTf:self.tfPass];
}

@end
